package com.lxw.oa.service.serviceImpl;

import com.lxw.oa.entity.User;
import com.lxw.oa.mapper.UserMapper;
import com.lxw.oa.service.LoginService;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("loginService")
@Transactional
public class LoginServiceImpl implements LoginService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findUserByName(String name) {
        return userMapper.findUserByName(name);
    }

    @Override
    public User loginCheck(String name, String password) {
        User user=new User();
        List<User> userList=findUserByName(name);
        if(userList.size()==0){
            throw new UnknownAccountException("该用户名不存在");
        }else {
            user.setName(name);
            user.setPassword(password);
            userList=userMapper.getUser(user);
            if (userList.size()==0){
                throw new IncorrectCredentialsException("用户名与密码不匹配");
            }else {
                user=userList.get(0);
            }
        }
        return user;
    }
}
